// #define print(...)
class Solution {
public:
    string convert(string s, int numRows) {
        int nRow = min((int)s.size(), numRows);
        if(nRow==1) return s;
        
        vector<string> rows(nRow);
        auto it = rows.begin();
        it->reserve(s.size());
        while(++it != rows.end()) it->reserve(s.size()/nRow+1);
        
        int i=0, step=-1;
        for(auto &c:s){
            rows[i].append(1, c);
            if(0==i || i==nRow-1)
                step = -step;
            i += step;
        }
        
        auto rs = rows.begin();
        for(auto it=rs+1; it != rows.end(); it++){
            rs->insert(rs->end(), it->begin(), it->end());
        }
        return *rs;
    }
};
